package edu.uoc.tds.pac4.client.ss4;

import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JPanel;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Vector;
import java.util.Date;

import javax.swing.JButton;

import edu.uoc.tds.pac4.beans.TipusEvent;
import edu.uoc.tds.pac4.client.ss4.Helpers;
import edu.uoc.tds.pac4.client.ss4.InterfaceFiltreInscripcions;
import edu.uoc.tds.pac4.common.TDSLanguageUtils;

public class JPanelFiltreConsultaEvents extends JPanel {

	private static final long serialVersionUID = 1L;
	protected static final int defaultHeight = 20;	
	private JLabel jLabelTipusEvent = null;  
	private JLabel jLabelData = null;  
	private JComboBox jComboBoxTipusEvent = null;  
	private JTextField jTextFieldData = null; 
	private JButton jButtonFiltre = null;  
	private InterfaceFiltreInscripcions pantalla = null;

	/**
	 * This is the default constructor
	 */
	public JPanelFiltreConsultaEvents(InterfaceFiltreInscripcions pantalla) {
		this.pantalla = pantalla;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		GridBagConstraints gbcButtonFiltre = new GridBagConstraints();
		gbcButtonFiltre.gridx = 4;
		gbcButtonFiltre.gridy = 1;
		gbcButtonFiltre.insets = new Insets(5, 10, 5, 10);

		// TipusEvent
		GridBagConstraints gbcLblTipusEvent = new GridBagConstraints();
		gbcLblTipusEvent.gridx = 0;
		gbcLblTipusEvent.gridy = 1;
		gbcLblTipusEvent.anchor = GridBagConstraints.LINE_END;
		jLabelTipusEvent = new JLabel();
		jLabelTipusEvent.setText(TDSLanguageUtils.getMessage("Client.PantallaConsultaEvents.TipusEvent"));
		GridBagConstraints gbcComboBoxTipusEvent = new GridBagConstraints();
		gbcComboBoxTipusEvent.gridx = 1;
		gbcComboBoxTipusEvent.gridy = 1;
		gbcComboBoxTipusEvent.insets = new Insets(5, 10, 5, 10);
		// Data
		GridBagConstraints gbcLabelData = new GridBagConstraints();
		gbcLabelData.gridx = 2;
		gbcLabelData.gridy = 1;
		gbcLabelData.anchor = GridBagConstraints.LINE_END;
		jLabelData = new JLabel();
		jLabelData.setText(TDSLanguageUtils.getMessage("Client.PantallaConsultaEvents.Data"));
		GridBagConstraints gbcTextData = new GridBagConstraints();
		gbcTextData.gridx = 3;
		gbcTextData.gridy = 1;
		gbcTextData.insets = new Insets(5, 10, 5, 10);
		// JPanel
		this.setLayout(new GridBagLayout());
		this.setName("jPanelFiltre");		
		this.add(getJButtonFiltre(), gbcButtonFiltre);
		this.add(jLabelTipusEvent, gbcLblTipusEvent);
		this.add(getJComboBoxTipusEvent(), gbcComboBoxTipusEvent);
		this.add(jLabelData,gbcLabelData);
		this.add(getJTextFieldData(),gbcTextData);		
	}

	// Getters	

	/**
	 * Retorna El tipus d'event Seleccionat
	 * 
	 * @return
	 */
	public TipusEvent getTipusEvent() {
		TipusEvent currentTipusEvent = null;
		if (jComboBoxTipusEvent.getSelectedItem() instanceof TipusEvent)
			currentTipusEvent = (TipusEvent) jComboBoxTipusEvent.getSelectedItem();
		else
			currentTipusEvent = null;

		return currentTipusEvent;
	}

	/**
	 * Retorna la Data 
	 * @return Date
	 */
	public Date getData() {
		SimpleDateFormat formatFecha = new SimpleDateFormat("dd/MM/yyyy"); 
		Date currentData = null;
		if (!jTextFieldData.getText().isEmpty()){
			try {
				currentData = formatFecha.parse(jTextFieldData.getText());
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return currentData;
	}	

	/**
	 * This method initializes jButtonFiltre
	 * @return javax.swing.JButton
	 */
	private JButton getJButtonFiltre() {
		if (jButtonFiltre == null) {
			jButtonFiltre = new JButton(TDSLanguageUtils.getMessage("Client.PantallaConsultaEvents.Filtrar"));
			jButtonFiltre.setSize(200, 20);
			jButtonFiltre.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					//Actualitzem taula llistat events
					pantalla.updateTaula();					
				}
			});
		}
		return jButtonFiltre;
	}

	/**
	 * This method initializes jComboBoxTipusEvent 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJComboBoxTipusEvent() {
		if (jComboBoxTipusEvent == null) {
			jComboBoxTipusEvent = new JComboBox();
			jComboBoxTipusEvent.setMinimumSize(new Dimension(200, 20));
			jComboBoxTipusEvent.setPreferredSize(new Dimension(200, 20));
			jComboBoxTipusEvent.addItem(TDSLanguageUtils.getMessage("Client.PantallaConsultaEvents.Tots"));
			Vector<TipusEvent> TipusEvents = pantalla.carregarTipusEvents();
			Helpers.setItemsCombo(TipusEvents, jComboBoxTipusEvent);			
		}
		return jComboBoxTipusEvent;
	}

	/**
	 * This method initializes jTextFieldData 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextFieldData() {
		if (jTextFieldData == null) {
			jTextFieldData = new JTextField();
			jTextFieldData
			.setMinimumSize(new Dimension(80, defaultHeight));
			jTextFieldData.setPreferredSize(new Dimension(80, defaultHeight));
		}
		return jTextFieldData;
	}		
}